Paul O'Leary
Comp 4433
Assignment 4 - Choropleths with the SturmData
This data set, from "Married Women's Economic Rights Reform, 1835-1920" details the years in which various state laws were passed to extend or grant women certain rights in a marriage prior to the Nineteenth Ammendment.
NOTE: Initially, the code to save the notebook as an HTML file was not displaying the graphs - blank areas appeared instead. I added code to save individual graphs. Now, it appears the HTML is saving properly. I will leave the individual writes in place.
IF the HTML file does not display the graphs, please let me know. Or the notebook can be run as wel, which will create a bunch of individual graphs. Thanks.
# Load some of the usual library suspects.
import plotly
import json
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import geopandas as gpd
import json
import pandas as pd
import altair as alt
import numpy as np
import matplotlib.pyplot as plt
# Load the data
sturm = pd.read_csv("SturmData_csv.txt")
# Peek at the data
sturm
| state | fips | icpsr | debtfree | effectivemwpa | earnings | wills | soletrader | |
|---|---|---|---|---|---|---|---|---|
| 0 | AL | 1 | 41 | 1846.0 | NaN | 1887.0 | NaN | NaN |
| 1 | AR | 5 | 42 | 1835.0 | 1873.0 | 1873.0 | 1868.0 | 1868.0 |
| 2 | AZ | 4 | 61 | 1864.0 | 1871.0 | 1973.0 | NaN | 1871.0 |
| 3 | CA | 6 | 71 | 1850.0 | 1872.0 | 1872.0 | 1874.0 | 1872.0 |
| 4 | CO | 8 | 62 | 1861.0 | 1861.0 | 1861.0 | 1874.0 | 1874.0 |
| 5 | CT | 9 | 1 | 1845.0 | 1877.0 | 1877.0 | 1809.0 | 1877.0 |
| 6 | DE | 10 | 11 | 1865.0 | 1873.0 | 1873.0 | NaN | NaN |
| 7 | FL | 12 | 43 | 1845.0 | 1943.0 | 1892.0 | 1823.0 | NaN |
| 8 | GA | 13 | 44 | 1868.0 | 1873.0 | 1861.0 | 1867.0 | NaN |
| 9 | IA | 19 | 31 | 1846.0 | 1873.0 | 1873.0 | NaN | 1873.0 |
| 10 | ID | 16 | 63 | 1887.0 | 1903.0 | 1915.0 | 1887.0 | 1887.0 |
| 11 | IL | 17 | 21 | 1861.0 | 1861.0 | 1869.0 | 1872.0 | 1874.0 |
| 12 | IN | 18 | 22 | 1847.0 | 1879.0 | 1879.0 | 1859.0 | NaN |
| 13 | KS | 20 | 32 | 1858.0 | 1858.0 | 1858.0 | 1858.0 | 1868.0 |
| 14 | KY | 21 | 51 | 1846.0 | 1894.0 | 1873.0 | NaN | 1873.0 |
| 15 | LA | 22 | 45 | NaN | 1916.0 | 1928.0 | NaN | 1894.0 |
| 16 | MA | 25 | 3 | 1855.0 | 1855.0 | 1846.0 | 1855.0 | 1860.0 |
| 17 | MD | 24 | 52 | 1842.0 | 1860.0 | 1842.0 | NaN | 1860.0 |
| 18 | ME | 23 | 2 | 1844.0 | 1855.0 | 1857.0 | NaN | 1844.0 |
| 19 | MI | 26 | 23 | 1844.0 | 1855.0 | 1911.0 | 1850.0 | NaN |
| 20 | MN | 27 | 33 | 1866.0 | 1869.0 | 1869.0 | 1869.0 | 1874.0 |
| 21 | MO | 29 | 34 | 1849.0 | 1875.0 | 1875.0 | NaN | NaN |
| 22 | MS | 28 | 46 | 1839.0 | 1880.0 | 1871.0 | 1880.0 | 1871.0 |
| 23 | MT | 30 | 64 | 1864.0 | 1887.0 | 1887.0 | NaN | 1874.0 |
| 24 | NC | 37 | 47 | 1849.0 | 1868.0 | 1913.0 | NaN | NaN |
| 25 | ND | 38 | 36 | 1862.0 | 1877.0 | NaN | 1877.0 | 1877.0 |
| 26 | NE | 31 | 35 | 1855.0 | 1871.0 | 1871.0 | 1897.0 | 1881.0 |
| 27 | NH | 33 | 4 | 1846.0 | 1860.0 | 1867.0 | 1854.0 | 1876.0 |
| 28 | NJ | 34 | 12 | 1852.0 | 1852.0 | 1874.0 | 1864.0 | 1874.0 |
| 29 | NM | 35 | 66 | 1884.0 | 1884.0 | NaN | NaN | NaN |
| 30 | NV | 32 | 65 | 1873.0 | 1873.0 | 1873.0 | 1873.0 | 1873.0 |
| 31 | NY | 36 | 13 | 1848.0 | 1848.0 | 1860.0 | 1849.0 | 1860.0 |
| 32 | OH | 39 | 24 | 1846.0 | 1861.0 | 1861.0 | 1808.0 | NaN |
| 33 | OK | 40 | 53 | 1890.0 | 1890.0 | NaN | NaN | NaN |
| 34 | OR | 41 | 72 | 1857.0 | 1878.0 | 1872.0 | 1853.0 | 1880.0 |
| 35 | PA | 42 | 14 | 1848.0 | 1848.0 | 1872.0 | 1848.0 | NaN |
| 36 | RI | 44 | 5 | 1844.0 | 1872.0 | 1872.0 | 1856.0 | NaN |
| 37 | SC | 45 | 48 | 1868.0 | 1868.0 | 1887.0 | 1870.0 | 1870.0 |
| 38 | SD | 46 | 37 | 1862.0 | 1877.0 | NaN | 1887.0 | 1877.0 |
| 39 | TN | 47 | 54 | 1850.0 | 1919.0 | 1919.0 | 1852.0 | NaN |
| 40 | TX | 48 | 49 | 1845.0 | 1913.0 | 1913.0 | 1895.0 | NaN |
| 41 | UT | 49 | 67 | 1872.0 | 1872.0 | 1897.0 | 1895.0 | 1895.0 |
| 42 | VA | 51 | 40 | 1877.0 | 1877.0 | 1888.0 | NaN | NaN |
| 43 | VT | 50 | 6 | 1845.0 | 1881.0 | 1888.0 | NaN | 1881.0 |
| 44 | WA | 53 | 73 | 1860.0 | 1881.0 | 1881.0 | 1889.0 | 1889.0 |
| 45 | WI | 55 | 25 | 1850.0 | 1850.0 | 1872.0 | NaN | NaN |
| 46 | WV | 54 | 56 | 1868.0 | 1868.0 | 1893.0 | 1882.0 | 1893.0 |
| 47 | WY | 56 | 68 | 1869.0 | 1869.0 | 1869.0 | 1869.0 | 1869.0 |
sturm.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 48 entries, 0 to 47 Data columns (total 8 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 state 48 non-null object 1 fips 48 non-null int64 2 icpsr 48 non-null int64 3 debtfree 47 non-null float64 4 effectivemwpa 47 non-null float64 5 earnings 44 non-null float64 6 wills 32 non-null float64 7 soletrader 31 non-null float64 dtypes: float64(5), int64(2), object(1) memory usage: 3.1+ KB
Nulls or NaNs exist in years-passed columns.
Plotly Express allows for choropleths to be displayed quite easily, but without much flexibility.
labels = {"debtfree":"Yr. Debt Free Law", "locations":"State"}
fig1 = px.choropleth(sturm.debtfree, locations=sturm.state, locationmode="USA-states",
scope="usa", color='debtfree', color_continuous_scale="blues",
title="Year, State Law passage Protecting Women's Separate Property from Husband - Interactive",
hover_name="debtfree", labels=labels) #, hover_data="debtfree")
fig1.show()
fig1.write_html('debtfree.html')
This choropleth clearly demonstrates the power of such a graph to display data concisely. However, the color scheme selected does not immediately make clear that Louisiana HAD NOT passed such a law prior to the Nineteenth Ammendment - the color is too close to states with actual data.
Here is the same choropleth with a different color scheme:
labels = {"debtfree":"Yr. Debt Free Law", "locations":"State"}
fig1 = px.choropleth(sturm.debtfree, locations=sturm.state, locationmode="USA-states",
scope="usa", color='debtfree', color_continuous_scale="speed",
title="Year, State Law passage Protecting Women's Separate Property from Husband - Interactive",
hover_name="debtfree", labels=labels) #, hover_data="debtfree")
fig1.show()
fig1.write_html('debtfree2.html')
In this case, the gray for no data clearly shows that no such law existed prior to the Nineteenth Ammendment.
This choropleth shows the year a state law was passed to grant married women control and management rights over their separate property.
labels = {"effectivemwpa":"Yr Mgmt Rights over Women's Property", "locations":"State"}
fig1 = px.choropleth(sturm.effectivemwpa, locations=sturm.state, locationmode="USA-states",
scope="usa", color='effectivemwpa', color_continuous_scale="speed",
title="Year, State Law passage Protecting Women's Control and Mgmt of Separate Property - Interactive",
hover_name="effectivemwpa", labels=labels) #, hover_data="debtfree")
fig1.show()
fig1.write_html('effectivemwpa.html')
Again, Alabama not having passed such a law is obvious.
Let's try other color schemes.
Available color schemes: ['aggrnyl', 'agsunset', 'algae', 'amp', 'armyrose', 'balance', 'blackbody', 'bluered', 'blues', 'blugrn', 'bluyl', 'brbg', 'brwnyl', 'bugn', 'bupu', 'burg', 'burgyl', 'cividis', 'curl', 'darkmint', 'deep', 'delta', 'dense', 'earth', 'edge', 'electric', 'emrld', 'fall', 'geyser', 'gnbu', 'gray', 'greens', 'greys', 'haline', 'hot', 'hsv', 'ice', 'icefire', 'inferno', 'jet', 'magenta', 'magma', 'matter', 'mint', 'mrybm', 'mygbm', 'oranges', 'orrd', 'oryel', 'oxy', 'peach', 'phase', 'picnic', 'pinkyl', 'piyg', 'plasma', 'plotly3', 'portland', 'prgn', 'pubu', 'pubugn', 'puor', 'purd', 'purp', 'purples', 'purpor', 'rainbow', 'rdbu', 'rdgy', 'rdpu', 'rdylbu', 'rdylgn', 'redor', 'reds', 'solar', 'spectral', 'speed', 'sunset', 'sunsetdark', 'teal', 'tealgrn', 'tealrose', 'tempo', 'temps', 'thermal', 'tropic', 'turbid', 'turbo', 'twilight', 'viridis', 'ylgn', 'ylgnbu', 'ylorbr', 'ylorrd'].
This choropleth shows year of passage of state law granting married women ownership of their wages or earnings on par with other separate property.
labels = {"earnings":"Yr Women's Rights over Earnings", "locations":"State"}
fig1 = px.choropleth(sturm.earnings, locations=sturm.state, locationmode="USA-states",
scope="usa", color='earnings', color_continuous_scale="rdpu",
title="Year, State Law passage Protecting Women's Rights over Earnings and Wages - Interactive",
hover_name="earnings", labels=labels) #, hover_data="debtfree")
fig1.show()
fig1.write_html('earnings.html')
In this case, four states: New Mexico, Oklahoma, North Dakota and South Dakota had not passed a law explicitly granting women rights over their separate earnings and wages.
labels = {"wills":"Yr Women's Right to Write a Will w/o Restrictions", "locations":"State"}
fig1 = px.choropleth(sturm.wills, locations=sturm.state, locationmode="USA-states",
scope="usa", color='wills', color_continuous_scale="rdpu",
title="Year, State Law passage Protecting Women's Right to Write a Will w/o Restriction - Interactive",
hover_name="wills", labels=labels)
fig1.show()
fig1.write_html('wills.html')
labels = {"soletrader":"Yr Women's Right to Sign Contracts", "locations":"State"}
fig1 = px.choropleth(sturm.soletrader, locations=sturm.state, locationmode="USA-states",
scope="usa", color='soletrader', color_continuous_scale="redor",
title="Year, State Law passage Protecting Women's Right Sign Contracts w/o Husband's Consent - Interactive",
hover_name="soletrader", labels=labels)
fig1.show()
fig1.write_html('soletrader.html')
For HTML Docs and other presentation methods that allow for color, this method of presentation is sufficient to convey the intended information. However, work is often presented in grayscale in printed publications. Plotly Express does provide grayscale color schemes:
labels = {"soletrader":"Yr Women's Right to Sign Contracts", "locations":"State"}
fig1 = px.choropleth(sturm.soletrader, locations=sturm.state, locationmode="USA-states",
scope="usa", color='soletrader', color_continuous_scale="gray",
title="Year, State Law passage Protecting Women's Right Sign Contracts w/o Husband's Consent - Interactive",
hover_name="soletrader", labels=labels)
fig1.show()
fig1.write_html('soletrader_gray1.html')
or:
labels = {"soletrader":"Yr Women's Right to Sign Contracts", "locations":"State"}
fig1 = px.choropleth(sturm.soletrader, locations=sturm.state, locationmode="USA-states",
scope="usa", color='soletrader', color_continuous_scale="greys",
title="Year, State Law passage Protecting Women's Right Sign Contracts w/o Husband's Consent - Interactive",
hover_name="soletrader", labels=labels)
fig1.show()
fig1.write_html('soletrader_gray2.html')
These greyscale options still do a fair job of conveying the idea that some states never did pass such laws.
A somewhat Kludgy way to emphasize that a law was not passed by a particular state would be to state that in the Hover data on the interactive map. This would assist on interactive versions of the maps to drive home that the state did not do anything prior to being forced by the Nineteenth Ammendment.
Here's one way to do it - for this example, I will focus on the "wills" column:
sturm2 = sturm.copy()
sturm2
| state | fips | icpsr | debtfree | effectivemwpa | earnings | wills | soletrader | |
|---|---|---|---|---|---|---|---|---|
| 0 | AL | 1 | 41 | 1846.0 | NaN | 1887.0 | NaN | NaN |
| 1 | AR | 5 | 42 | 1835.0 | 1873.0 | 1873.0 | 1868.0 | 1868.0 |
| 2 | AZ | 4 | 61 | 1864.0 | 1871.0 | 1973.0 | NaN | 1871.0 |
| 3 | CA | 6 | 71 | 1850.0 | 1872.0 | 1872.0 | 1874.0 | 1872.0 |
| 4 | CO | 8 | 62 | 1861.0 | 1861.0 | 1861.0 | 1874.0 | 1874.0 |
| 5 | CT | 9 | 1 | 1845.0 | 1877.0 | 1877.0 | 1809.0 | 1877.0 |
| 6 | DE | 10 | 11 | 1865.0 | 1873.0 | 1873.0 | NaN | NaN |
| 7 | FL | 12 | 43 | 1845.0 | 1943.0 | 1892.0 | 1823.0 | NaN |
| 8 | GA | 13 | 44 | 1868.0 | 1873.0 | 1861.0 | 1867.0 | NaN |
| 9 | IA | 19 | 31 | 1846.0 | 1873.0 | 1873.0 | NaN | 1873.0 |
| 10 | ID | 16 | 63 | 1887.0 | 1903.0 | 1915.0 | 1887.0 | 1887.0 |
| 11 | IL | 17 | 21 | 1861.0 | 1861.0 | 1869.0 | 1872.0 | 1874.0 |
| 12 | IN | 18 | 22 | 1847.0 | 1879.0 | 1879.0 | 1859.0 | NaN |
| 13 | KS | 20 | 32 | 1858.0 | 1858.0 | 1858.0 | 1858.0 | 1868.0 |
| 14 | KY | 21 | 51 | 1846.0 | 1894.0 | 1873.0 | NaN | 1873.0 |
| 15 | LA | 22 | 45 | NaN | 1916.0 | 1928.0 | NaN | 1894.0 |
| 16 | MA | 25 | 3 | 1855.0 | 1855.0 | 1846.0 | 1855.0 | 1860.0 |
| 17 | MD | 24 | 52 | 1842.0 | 1860.0 | 1842.0 | NaN | 1860.0 |
| 18 | ME | 23 | 2 | 1844.0 | 1855.0 | 1857.0 | NaN | 1844.0 |
| 19 | MI | 26 | 23 | 1844.0 | 1855.0 | 1911.0 | 1850.0 | NaN |
| 20 | MN | 27 | 33 | 1866.0 | 1869.0 | 1869.0 | 1869.0 | 1874.0 |
| 21 | MO | 29 | 34 | 1849.0 | 1875.0 | 1875.0 | NaN | NaN |
| 22 | MS | 28 | 46 | 1839.0 | 1880.0 | 1871.0 | 1880.0 | 1871.0 |
| 23 | MT | 30 | 64 | 1864.0 | 1887.0 | 1887.0 | NaN | 1874.0 |
| 24 | NC | 37 | 47 | 1849.0 | 1868.0 | 1913.0 | NaN | NaN |
| 25 | ND | 38 | 36 | 1862.0 | 1877.0 | NaN | 1877.0 | 1877.0 |
| 26 | NE | 31 | 35 | 1855.0 | 1871.0 | 1871.0 | 1897.0 | 1881.0 |
| 27 | NH | 33 | 4 | 1846.0 | 1860.0 | 1867.0 | 1854.0 | 1876.0 |
| 28 | NJ | 34 | 12 | 1852.0 | 1852.0 | 1874.0 | 1864.0 | 1874.0 |
| 29 | NM | 35 | 66 | 1884.0 | 1884.0 | NaN | NaN | NaN |
| 30 | NV | 32 | 65 | 1873.0 | 1873.0 | 1873.0 | 1873.0 | 1873.0 |
| 31 | NY | 36 | 13 | 1848.0 | 1848.0 | 1860.0 | 1849.0 | 1860.0 |
| 32 | OH | 39 | 24 | 1846.0 | 1861.0 | 1861.0 | 1808.0 | NaN |
| 33 | OK | 40 | 53 | 1890.0 | 1890.0 | NaN | NaN | NaN |
| 34 | OR | 41 | 72 | 1857.0 | 1878.0 | 1872.0 | 1853.0 | 1880.0 |
| 35 | PA | 42 | 14 | 1848.0 | 1848.0 | 1872.0 | 1848.0 | NaN |
| 36 | RI | 44 | 5 | 1844.0 | 1872.0 | 1872.0 | 1856.0 | NaN |
| 37 | SC | 45 | 48 | 1868.0 | 1868.0 | 1887.0 | 1870.0 | 1870.0 |
| 38 | SD | 46 | 37 | 1862.0 | 1877.0 | NaN | 1887.0 | 1877.0 |
| 39 | TN | 47 | 54 | 1850.0 | 1919.0 | 1919.0 | 1852.0 | NaN |
| 40 | TX | 48 | 49 | 1845.0 | 1913.0 | 1913.0 | 1895.0 | NaN |
| 41 | UT | 49 | 67 | 1872.0 | 1872.0 | 1897.0 | 1895.0 | 1895.0 |
| 42 | VA | 51 | 40 | 1877.0 | 1877.0 | 1888.0 | NaN | NaN |
| 43 | VT | 50 | 6 | 1845.0 | 1881.0 | 1888.0 | NaN | 1881.0 |
| 44 | WA | 53 | 73 | 1860.0 | 1881.0 | 1881.0 | 1889.0 | 1889.0 |
| 45 | WI | 55 | 25 | 1850.0 | 1850.0 | 1872.0 | NaN | NaN |
| 46 | WV | 54 | 56 | 1868.0 | 1868.0 | 1893.0 | 1882.0 | 1893.0 |
| 47 | WY | 56 | 68 | 1869.0 | 1869.0 | 1869.0 | 1869.0 | 1869.0 |
sturm2.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 48 entries, 0 to 47 Data columns (total 8 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 state 48 non-null object 1 fips 48 non-null int64 2 icpsr 48 non-null int64 3 debtfree 47 non-null float64 4 effectivemwpa 47 non-null float64 5 earnings 44 non-null float64 6 wills 32 non-null float64 7 soletrader 31 non-null float64 dtypes: float64(5), int64(2), object(1) memory usage: 3.1+ KB
First, let's convert the floats to integers for the year.
sturm2['wills'] = sturm2['wills'].fillna(0.0).astype(int)
sturm2.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 48 entries, 0 to 47 Data columns (total 8 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 state 48 non-null object 1 fips 48 non-null int64 2 icpsr 48 non-null int64 3 debtfree 47 non-null float64 4 effectivemwpa 47 non-null float64 5 earnings 44 non-null float64 6 wills 48 non-null int64 7 soletrader 31 non-null float64 dtypes: float64(4), int64(3), object(1) memory usage: 3.1+ KB
sturm2.head()
| state | fips | icpsr | debtfree | effectivemwpa | earnings | wills | soletrader | |
|---|---|---|---|---|---|---|---|---|
| 0 | AL | 1 | 41 | 1846.0 | NaN | 1887.0 | 0 | NaN |
| 1 | AR | 5 | 42 | 1835.0 | 1873.0 | 1873.0 | 1868 | 1868.0 |
| 2 | AZ | 4 | 61 | 1864.0 | 1871.0 | 1973.0 | 0 | 1871.0 |
| 3 | CA | 6 | 71 | 1850.0 | 1872.0 | 1872.0 | 1874 | 1872.0 |
| 4 | CO | 8 | 62 | 1861.0 | 1861.0 | 1861.0 | 1874 | 1874.0 |
sturm2['text'] = sturm2['wills'].astype(str)
sturm2.head()
| state | fips | icpsr | debtfree | effectivemwpa | earnings | wills | soletrader | text | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | AL | 1 | 41 | 1846.0 | NaN | 1887.0 | 0 | NaN | 0 |
| 1 | AR | 5 | 42 | 1835.0 | 1873.0 | 1873.0 | 1868 | 1868.0 | 1868 |
| 2 | AZ | 4 | 61 | 1864.0 | 1871.0 | 1973.0 | 0 | 1871.0 | 0 |
| 3 | CA | 6 | 71 | 1850.0 | 1872.0 | 1872.0 | 1874 | 1872.0 | 1874 |
| 4 | CO | 8 | 62 | 1861.0 | 1861.0 | 1861.0 | 1874 | 1874.0 | 1874 |
sturm2['text'] = sturm2['text'].replace({'0':'Not Passed'}) #, regex=True)
# regex replaced 0s in the middle of dates with 'Not Passed'!! Removed and it worked.
sturm2.head()
| state | fips | icpsr | debtfree | effectivemwpa | earnings | wills | soletrader | text | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | AL | 1 | 41 | 1846.0 | NaN | 1887.0 | 0 | NaN | Not Passed |
| 1 | AR | 5 | 42 | 1835.0 | 1873.0 | 1873.0 | 1868 | 1868.0 | 1868 |
| 2 | AZ | 4 | 61 | 1864.0 | 1871.0 | 1973.0 | 0 | 1871.0 | Not Passed |
| 3 | CA | 6 | 71 | 1850.0 | 1872.0 | 1872.0 | 1874 | 1872.0 | 1874 |
| 4 | CO | 8 | 62 | 1861.0 | 1861.0 | 1861.0 | 1874 | 1874.0 | 1874 |
labels = {"wills":"Yr Women's Right to Write a Will w/o Restrictions", "locations":"State"}
fig1 = px.choropleth(sturm2.wills, locations=sturm2.state, locationmode="USA-states",
scope="usa", color='wills', color_continuous_scale="rdpu", range_color=[1800, 1920],
title="Year, State Law passage Protecting Women's Right to Write a Will w/o Restriction - Interactive",
hover_name=sturm2.text, labels=labels)
fig1.show()
fig1.write_html('wills_Not_Passed_hover.html')
And now in grayscale.
labels = {"wills":"Yr Women's Right to Write a Will w/o Restrictions", "locations":"State"}
fig1 = px.choropleth(sturm2.wills, locations=sturm2.state, locationmode="USA-states",
scope="usa", color='wills', color_continuous_scale="greys", range_color=[1800, 1920],
title="Year, State Law passage Protecting Women's Right to Write a Will w/o Restriction - Interactive",
hover_name=sturm2.text, labels=labels)
fig1.show()
fig1.write_html('wills_Not_Passed_Hover_gray.html')
One more time for another example, this time with the "earnings" column.
sturm2.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 48 entries, 0 to 47 Data columns (total 9 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 state 48 non-null object 1 fips 48 non-null int64 2 icpsr 48 non-null int64 3 debtfree 47 non-null float64 4 effectivemwpa 47 non-null float64 5 earnings 44 non-null float64 6 wills 48 non-null int64 7 soletrader 31 non-null float64 8 text 48 non-null object dtypes: float64(4), int64(3), object(2) memory usage: 3.5+ KB
sturm2['earnings'] = sturm2['earnings'].fillna(0.0).astype(int)
sturm2['text2'] = sturm2['earnings'].astype(str)
sturm2.head()
| state | fips | icpsr | debtfree | effectivemwpa | earnings | wills | soletrader | text | text2 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | AL | 1 | 41 | 1846.0 | NaN | 1887 | 0 | NaN | Not Passed | 1887 |
| 1 | AR | 5 | 42 | 1835.0 | 1873.0 | 1873 | 1868 | 1868.0 | 1868 | 1873 |
| 2 | AZ | 4 | 61 | 1864.0 | 1871.0 | 1973 | 0 | 1871.0 | Not Passed | 1973 |
| 3 | CA | 6 | 71 | 1850.0 | 1872.0 | 1872 | 1874 | 1872.0 | 1874 | 1872 |
| 4 | CO | 8 | 62 | 1861.0 | 1861.0 | 1861 | 1874 | 1874.0 | 1874 | 1861 |
sturm2['text2'] = sturm2['text2'].replace({'0':'Not Passed'})
# sturm2
labels = {"earnings":"Yr Women's Rights over Earnings", "locations":"State"}
fig1 = px.choropleth(sturm2.earnings, locations=sturm2.state, locationmode="USA-states",
scope="usa", color='earnings', color_continuous_scale="rdpu", range_color=[1800, 1920],
title="Year, State Law passage Protecting Women's Rights over Earnings and Wages - Interactive",
hover_name=sturm2.text2, labels=labels) #, hover_data="debtfree")
fig1.show()
fig1.write_html('earnings_Not_Passed_Hover.html')
And in grayscale.
labels = {"earnings":"Yr Women's Rights over Earnings", "locations":"State"}
fig1 = px.choropleth(sturm2.earnings, locations=sturm2.state, locationmode="USA-states",
scope="usa", color='earnings', color_continuous_scale="greys", range_color=[1800, 1920],
title="Year, State Law passage Protecting Women's Rights over Earnings and Wages - Interactive",
hover_name=sturm2.text2, labels=labels) #, hover_data="debtfree")
fig1.show()
fig1.write_html('earnings_Not_Passed_Hover_gray.html')
Write out this notebook as an html file.
import os
os.system('jupyter nbconvert --to html Paul_OLeary_Assign4.ipynb')
0
Not properly converting the graphs to interactive images within the bigger HTML Doc.
As a stop gap, I will right out each individual graph, which seems to work.
FAILED ATTEMPT BELOW
# Let's do 'wills' for now - drop the NaN values in the wills column
sturm3 = sturm[sturm['wills'].notna()]
sturm3.info()
<class 'pandas.core.frame.DataFrame'> Int64Index: 32 entries, 1 to 47 Data columns (total 8 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 state 32 non-null object 1 fips 32 non-null int64 2 icpsr 32 non-null int64 3 debtfree 32 non-null float64 4 effectivemwpa 32 non-null float64 5 earnings 30 non-null float64 6 wills 32 non-null float64 7 soletrader 23 non-null float64 dtypes: float64(5), int64(2), object(1) memory usage: 2.2+ KB
# Example to do something with the NaNs. Overlay different plots for real data and then NaNs??
import chart_studio.plotly as py
import pandas as pd
# Filter NA here
# df = df.dropna()
data = [ dict(
type = 'choropleth',
locations = sturm2['state'],
z = sturm3['wills'],
text = sturm3['state'],
# color = sturm3['state'],
colorscale = 'Viridis',
autocolorscale = False,
reversescale = True,
marker = dict(
line = dict (
color = 'rgb(180,180,180)',
width = 0.5
) ),
colorbar = dict(
tickprefix = '',
title = 'test test'),
) ]
layout = dict(
title = 'next test',
geo = dict(
landcolor = 'lightgray',
showland = True,
showcountries = True,
# statecolor = 'gray',
scope = "usa",
countrywidth = 0.5,
projection = dict(
type = 'albers usa'
)
)
)
fig = go.FigureWidget( data=data, layout=layout )
fig
# ! pip install chart_studio